package com.itheima.entity;

import com.hzheima.util.ImageUtil;
import com.itheima.config.AppConfig;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;

public class Ball extends FlyingObject {
    private int xSpeed;
    private int ySpeed;

    private int starLoc;
    private boolean startDown;

    private int blood;
    private int initBlood;


    public Ball(BufferedImage img, int x, int y, int width, int hight) {
        super(img, x, y, width, hight);
    }

    public Ball(int xSpeed, int ySpeed) {
        this.xSpeed = xSpeed;
        this.ySpeed = ySpeed;
    }

    public Ball(BufferedImage img, int x, int y, int width, int hight, int xSpeed, int ySpeed) {
        super(img, x, y, width, hight);
        this.xSpeed = xSpeed;
        this.ySpeed = ySpeed;
    }




    public int getInitBlood() {
        return initBlood;
    }

    public Ball() {
        //随机获取小球图片
        Random random=new Random();
        int imgNum=random.nextInt(15);
        String imgName="c"+imgNum+".png";
        img= ImageUtil.getImg(imgName);

        width=img.getWidth();
        hight=img.getHeight();

        //随机获取小球起始坐标
        starLoc = random.nextInt(2);
        if(starLoc==0){
            x=-width;
            xSpeed=3;
        }else if(starLoc==1){
            x= AppConfig.App_WIDTH;
            xSpeed=-3;
        }
        y=100;
        ySpeed=5;

        blood=random.nextInt(15)+1;
        initBlood=blood;
    }


    public void move(){
        x+=xSpeed;
        if(starLoc==AppConfig.LEFT){
            if(x>=AppConfig.App_WIDTH/2-width){
               startDown=true;
            }

        }else if( starLoc==AppConfig.RIGHT){
            if(x<=AppConfig.App_WIDTH/2){
               startDown=true;
            }
        }

        if(startDown==true){
            y+=ySpeed;
        }

        //小球碰到墙壁反弹
        if(xSpeed<0&&x<=0){
            //撞到左边
            xSpeed=-xSpeed;
        }else if(xSpeed>0&&x>=AppConfig.App_WIDTH-width){
            //撞到右边
            xSpeed=-xSpeed;
        }else if(ySpeed>0&&y>=AppConfig.App_HIGHT-hight-40){
            // 撞到底部
            ySpeed=-ySpeed;
        }else if(ySpeed<0&&y<=0){
            //撞到顶部
            ySpeed=-ySpeed;
        }

    }

    public int getBlood() {
        return blood;
    }

    @Override
    public void draw(Graphics g) {
        //绘制小球
        super.draw(g);
        //设置小球血量的字体
        g.setColor(Color.white);
        //调整血量数值位置
        int dx=blood>=10?10:8;
        int dy=5;
        g.drawString(String.valueOf(blood),x+width/2-dx,y+hight/2+dy);

    }


    //判断是否被子弹击中
    public boolean beShoot(Bullet bullet){
        boolean b=collide(bullet);
        if(b){
            //被子弹击中,血量减一
            blood--;

            //被子弹打中球就开始下降
            startDown=true;
        }
        return b;
    }

    //分裂小球构造
    public Ball(int x ,int y,int ySpeed,int initBlood){
        //随机获取小球图片
        Random random=new Random();
        int imgNum=random.nextInt(15);
        String imgName="c"+imgNum+".png";
        img= ImageUtil.getImg(imgName);

        width=img.getWidth();
        hight=img.getHeight();

        this.x=x;
        this.y=y;

        this.ySpeed=ySpeed;
        this.xSpeed=random.nextInt(11)-5;

        this.initBlood=initBlood;
        this.blood=initBlood;

        startDown=true;
    }

    //分裂小球
    public ArrayList<Ball> split(){
        ArrayList<Ball> ballList = new ArrayList<>();
        if(this.blood>0) {
            Ball ball1 = new Ball(this.x, this.y,this.ySpeed, this.blood);
            Ball ball2 = new Ball(this.x, this.y ,this.ySpeed, this.blood);
            ballList.add(ball1);
            ballList.add(ball2);
        }
        return ballList;
    }
}
